<?php

class Materials_Model_Material_Attribute implements Workset_Model_Value_Form_Interface {

    protected $_value = null;

    protected $_name = null;

    protected $_type = Workset_Model_Value_Form_Interface::T_STRING;

    protected $_defaultValue = null;

    protected $_enum = false;

    protected $_multiple = false;

    protected $_required = Workset_Model_Value_Form_Interface::NOT_REQUIRED;

    protected $_htmlId = null;

    protected $_options = array();

    protected static $_counter = 0;

    function __construct($id, $value = null, $type = null) {
        $this->_htmlId = (string)$id;

        if (null !== $value) {
            $this->setValue($value);
        }

        if (null !== $type) {
            $this->_type = $type;
        }

    }

    static function getFileTypes() {
        return array(
            Workset_Model_Value_Form_Interface::T_FILE,
            Workset_Model_Value_Form_Interface::T_IMAGE,
            Workset_Model_Value_Form_Interface::T_VIDEO
        );
    }

	function setOptions($options) {
		if ($options instanceof Zend_Config) {
			$options = $options->toArray();
		}

		foreach ($options as $key => $value) {
			if (null === $value) {
				continue;
			}

			switch ($key) {
				case 'name':
					$this->setName($value);
				break;

				case 'isEnum':
					$this->setIsEnum($value);
				break;

				case 'isMultiple':
					$this->setIsMultiple($value);
				break;

				case 'isRequired':
					$this->setIsRequired($value);
				break;

				case 'value':
					$this->setValue($value);
				break;
			}

            unset($options[$key]);
		}

        if ($options) {
            $this->_options = $options;
        }
	}

    function setIsEnum($flag = true) {
        $this->_enum = (bool)$flag;
        
    }

    function isEnum() {
        return $this->_enum;
    }

    function setIsMultiple($flag = true) {
        $this->_multiple = (bool)$flag;
    }

    function isMultiple() {
        return $this->_multiple;
    }

    function isSelect() {
        return $this->typeOf(Workset_Model_Value_Form_Interface::T_SELECT);
    }

	function setIsRequired($level) {
		$this->_required = $level;
	}

    function isRequired($level = null) {
        if (null !== $level) {
            return $this->_required == $level;
        } else {
            $this->_required == Workset_Model_Value_Form_Interface::REQUIRED;
        }
    }

    function setHtmlId($id) {
        $this->_htmlId = (string)$id;
    }

    function getHtmlId($prefix = null, $postfix = null) {
		if (null !== $this->_htmlId) {
			$id = $this->_htmlId;
		} else {
			throw new Workset_Model_Exception('Undefined html id');
		}

		if (null !== $prefix) {
			$id = $prefix."[$id]";
		}

		if (null !== $postfix) {
			$id = $id."[$postfix]";
		}

        return $id;
    }

    function getModelId() {
        return 0; //$this->getHtmlId();
    }

    function setValue($value) {
        $this->_value = $value;
    }

    function getValue() {
        if (null !== $this->_value) {
            return $this->_value;
        } else {
            return $this->getDefaultValue();
        }
    }

    function setDefaultValue($value) {
        $this->_defaultValue = $value;
    }

    function getDefaultValue() {
        return $this->_defaultValue;
    }

    function setName($name) {
        $this->_name = (string)$name;
    }

    function getName() {
        return $this->_name;
    }

    function getEnumItems() {
        return null;
    }

    function render($content = null) {
        return $this->getValue();
    }

    function isInt() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_INT;
    }

    function isDecimal() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_DECIMAL;
    }

    function isString() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_STRING;
    }

    function isBool() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_BOOL;
    }

    function isText() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_TEXT;
    }

    function isFile() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_FILE
                ||
               ( in_array($this->getType(), self::getFileTypes()) );
    }

    function isImage() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_IMAGE;
    }

    function isVideo() {
        return $this->_type == Workset_Model_Value_Form_Interface::T_VIDEO;
    }

    function getType() {
        return $this->_type;
    }

    function typeOf($type) {

        if ($this->getType() == $type) {
            return true;
        }

        $success = false;

        switch ($type) {
            case self::T_FILE:
                $success = $this->isFile();
            break;

            case self::T_IMAGE:
                $success = $this->isImage();
            break;

            case self::T_VIDEO:
                $success = $this->isVideo();
            break;
        }

        return $success;
    }

    function getFieldType() {
        if ($this->typeOf(self::T_FILE)) {
            return 'file';

        } elseif ($this->typeOf(self::T_TEXT)) {
            return 'textarea';

        } elseif ($this->typeOf(self::T_BOOL)) {
            return 'checkbox';

        } elseif ($this->isEnum() || $this->typeOf(self::T_SELECT)) {
            return 'select';

        } else {
            return 'text';
            
        }
    }

    function getFieldId($belongsTo = null) {

        $id = $this->getHtmlId($belongsTo);

        if (!strstr($id, '[')) {
            return $id;
        }

        if ('[]' == substr($id, -2)) {
            $id = substr($id, 0, strlen($id) - 2);
        }
        
        $id = str_replace('][', '-', $id);
        $id = str_replace(array(']', '['), '-', $id);
        $id = trim($id, '-');

        return $id;
    }

}

?>